package com.hengpeng.itfin.service.impl;

import java.util.Date;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.exception.ServiceResultFactory;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.util.MoneyUtil;
import com.hengpeng.itfin.service.TransferService;
import com.hengpeng.itfinbase.constants.RespCodeConstants;
import com.hengpeng.itfinbase.en.AccountType;
import com.hengpeng.itfinbase.idAssign.ID;
import com.hengpeng.itfinbase.message.partner.TransferRequest;
import com.hengpeng.itfinbase.message.partner.TransferResponse;
import com.hengpeng.itfinbase.persist.AccountLog.TransType;
import com.hengpeng.itfinbase.persist.Transfer;
import com.hengpeng.itfinbase.persist.Transfer.TransferStatus;

/**
 * <pre>
 * 支付系统服务实现
 * </pre>
 * @author zhaojitao
 * @version 1.0, 2015-6-4
 */
@Service
@Transactional
public class TransferServiceImpl extends BaseServiceImpl implements TransferService
{

	@Override
	public Transfer findByOrderId(String orderId)
	{
		DynamicQuery query = new DynamicQuery(Transfer.class);
		query.eq("orderId", orderId);
		return serviceFactory.getPersistService().getByDynamicQuery(Transfer.class, query);
	}

	@Override
	public void packTransferRequest(TransferRequest request)
	{
		Transfer transfer = new Transfer();
//		String orderId = String.valueOf(serviceFactory.getSequenceService().getSequence(
//				SystemConfigUtil.getConfigValue(SystemConfigConstants.TRANSFER_SEQ)));
		String orderId = ID.getInstanse().getID(19).toString();
		request.setOrdId(orderId);
		transfer.setOrderId(orderId);
		transfer.setMoney(MoneyUtil.convertLongYuanToFen(Double.parseDouble(request.getTransAmt())));
		transfer.setStatus(TransferStatus.DEALING);

		//出账只能是平台账户，故暂时不计冻结流水
		transfer.setOutType(request.getOutType());
		transfer.setOutUserId(request.getOutUserId());
		transfer.setOutTrustAccountId(request.getOutCustId());
		transfer.setOutSubAccountId(request.getOutAcctId());

		
		transfer.setInType(request.getInType());
		transfer.setInUserId(request.getInUserId());
		transfer.setInTrustAccountId(request.getInCustId());
		transfer.setInSubAccountId(request.getInAcctId());

		transfer.setCreateTime(new Date());
		transfer.setUpdateTime(new Date());

		
		serviceFactory.getDaoFactory().getCommonDao().saveEntity(transfer);
		
	}

	@Override
	public ServiceResult<Transfer> dealTransferResult(TransferResponse msg)
	{
		Transfer transfer = serviceFactory.getTransferService().findByOrderId(msg.getOrdId());
		transfer = daoFactory.getCommonDao().lockObject(Transfer.class, transfer.getId());
		if (transfer != null)
		{
			if (transfer.getStatus() == TransferStatus.DEALING)
			{
				if (msg.getRespCode().equals(RespCodeConstants.CommonCode.TRANS_SUC))
				{
					transfer.setStatus(TransferStatus.SUCCESS);
					transfer.setUpdateTime(new Date());
					transfer.setRemark(msg.getRespCode()+":"+msg.getRespDesc());
					if(transfer.getInType()==AccountType.PERSONAL)
					{//只操作个人用户，平台用户暂不做本地资金操作
						serviceFactory.getAccountService().add(transfer.getInUserId(), TransType.TRANSFER, transfer.getMoney(),String.valueOf(transfer.getId()), "转账成功");
					}
				}
				else
				{
					transfer.setStatus(TransferStatus.FAILED);
					transfer.setUpdateTime(new Date());
				}
			}
			serviceFactory.getDaoFactory().getCommonDao().updateEntity(transfer);
			return ServiceResultFactory.getServiceResult(RespCodeConstants.CommonCode.TRANS_SUC, transfer);
		}
		else
		{
			// 未找到对账记录
			return ServiceResultFactory.getServiceResult(RespCodeConstants.CommonCode.DATA_NOT_FOUND, Transfer.class);

		}
	}
}
